package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;
import com.kekezu.kppw.utils.StrFormat;

public class ClassifyData {
	/**
	 * 一级分类下的服务商
	 * 
	 * @param Strcate
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> serviceByCate(
			String Strcate, Integer page, Context context) {
		ArrayList<HashMap<String, Object>> aliList = new ArrayList<HashMap<String, Object>>();

		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/serviceByCate?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("cate_id", Strcate);
		params.put("page", "" + page);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONArray jsonArray = jsonObject.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					map.put("id", itemJsonObject.getString("id"));
					map.put("uid", itemJsonObject.getString("uid"));
					map.put("shop_pic", itemJsonObject.getString("shop_pic"));
					map.put("shop_name", itemJsonObject.getString("shop_name"));
					map.put("total_comment",
							itemJsonObject.getString("total_comment"));
					map.put("good_comment",
							itemJsonObject.getString("good_comment"));
					map.put("cate_name", itemJsonObject.getString("cate_name"));
					map.put("city_name", itemJsonObject.getString("city_name"));
					map.put("percent", "100" + "%");
					map.put("isEnterprise",
							itemJsonObject.getString("isEnterprise"));
					map.put("bank", itemJsonObject.getString("bank"));
					map.put("alipay", itemJsonObject.getString("alipay"));
					map.put("email", itemJsonObject.getString("email"));
					map.put("realname", itemJsonObject.getString("realname"));

					aliList.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return aliList;
	}

	/**
	 * 查看一级分类下的任务信息
	 * 
	 * @param Strcate
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> taskByCate(String Strcate,
			Integer page, Context context) {
		ArrayList<HashMap<String, Object>> aliList = new ArrayList<HashMap<String, Object>>();

		String urlString = ConfigInc.getServiceAdress(context)
				+ "task/taskByCate?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("cate_id", Strcate);
		// params.put("page", "" + page);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			// Log.e("strJson", strJson);
			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONArray jsonArray = jsonObject.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					map.put("id", itemJsonObject.getString("id"));
					map.put("title", itemJsonObject.getString("title"));
					map.put("view_count",
							itemJsonObject.getString("view_count"));
					map.put("delivery_count",
							itemJsonObject.getString("delivery_count"));
					map.put("created_at", itemJsonObject
							.getString("created_at").substring(0, 10));
					map.put("bounty", itemJsonObject.getString("bounty"));
					map.put("bounty_status",
							itemJsonObject.getString("bounty_status"));// 托管状态
																		// 1:已托管
																		// 0:未托管
					map.put("bounty_status_desc",
							itemJsonObject.getString("bounty_status_desc"));

					if (StrFormat.formatStr(itemJsonObject.getString("name"))) {
						map.put("cate_name", itemJsonObject.getString("name"));
					} else {
						map.put("cate_name", "暂无");
					}
					map.put("uid", itemJsonObject.getString("uid"));
					map.put("task_type", itemJsonObject.getString("task_type"));
					map.put("task_service",
							itemJsonObject.getString("task_service"));

					aliList.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return aliList;
	}
}
